#!/bin/bash
#自动工具启动时初始化脚本
#参数：系统操作根路径
#dir=$(cd `dirname $0`; pwd)

error_exit(){
	echo "`date +%Y:%m:%d-%H:%M:%S` ERROR: $1" >> ${logFile}
	echo "ERROR: 初始化工作目录, $1"
	exit 1
}

rootPath=$1
logFile="${rootPath}/Beetle/init.log"
cd ${rootPath}

#判断文件目录是否存在
if [ -e Beetle ]
then
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir Beetle is exist " >> ${logFile}
else
	mkdir Beetle || error_exit "创建文件夹Beetle失败"
	chmod 770 Beetle/
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: 自动化部署工具初始化，创建工作目录 " >> ${logFile}
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir Beetle! " >> ${logFile}
fi

cd Beetle
#创建工程文件夹
if [ -e projects ]
then
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir projects is exist " >> ${logFile}
else
	mkdir projects || error_exit "创建文件夹projects失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir projects! " >> ${logFile}
fi
#创建用户文件夹
if [ -e users ]
then
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir users is exist " >> ${logFile}
else
	mkdir users || error_exit "创建文件夹users失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir users! " >> ${logFile}
fi

if [ -e ssh ]
then
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir ssh is exist " >> ${logFile}
else
	mkdir ssh || error_exit "创建文件夹ssh失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir ssh! " >> ${logFile}
fi
if [ -e jumpServer ]
then
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir jumpServer is exist " >> ${logFile}
else
	mkdir jumpServer || error_exit "创建文件夹jumpServer失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir jumpServer! " >> ${logFile}
fi
if [ -e pushRecord ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir pushRecord is exist " >> ${logFile}
else
	mkdir pushRecord || error_exit "创建文件夹pushRecord失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir pushRecord! " >> ${logFile}
fi
if [ -e svnResource ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir svnResource is exist " >> ${logFile}
else
	mkdir svnResource || error_exit "创建文件夹svnResource失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir svnResource! " >> ${logFile}
fi
if [ -e logs ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir logs is exist " >> ${logFile}
else
	mkdir logs || error_exit "创建文件夹logs失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir logs! " >> ${logFile}
fi
if [ -e release ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir release is exist " >> ${logFile}
else
	mkdir release || error_exit "创建文件夹release失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir release! " >> ${logFile}
fi
if [ -e vm ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir vm is exist " >> ${logFile}
else
	mkdir vm || error_exit "创建文件夹vm失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir vm! " >> ${logFile}
fi
if [ -e uCloud ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir uCloud is exist " >> ${logFile}
else
	mkdir uCloud || error_exit "创建文件夹uCloud失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir uCloud! " >> ${logFile}
fi
if [ -e temp ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir temp is exist " >> ${logFile}
else
	mkdir temp || error_exit "创建文件夹temp失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir temp! " >> ${logFile}
fi
cd users
if [ -e temp ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir users/temp is exist " >> ${logFile}
else
	mkdir temp || error_exit "创建文件夹users/temp失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir users/temp! " >> ${logFile}
fi
cd ../logs
if [ -e shell ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir logs/shell is exist " >> ${logFile}
else
	mkdir shell || error_exit "创建文件夹logs/shell失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir logs/shell! " >> ${logFile}
fi
cd ../release
if [ -e info ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir release/info is exist " >> ${logFile}
else
	mkdir info || error_exit "创建文件夹release/info失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir release/info! " >> ${logFile}
fi
if [ -e temp ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir release/temp is exist " >> ${logFile}
else
	mkdir temp || error_exit "创建文件夹release/temp失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir release/temp! " >> ${logFile}
fi
if [ -e log ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir release/log is exist " >> ${logFile}
else
	mkdir log || error_exit "创建文件夹release/log"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir release/log! " >> ${logFile}
fi
cd ../vm
if [ -e templates ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir vm/templates is exist " >> ${logFile}
else
	mkdir templates || error_exit "创建文件夹vm/templates失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir vm/templates! " >> ${logFile}
fi
if [ -e vms ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir vm/vms is exist " >> ${logFile}
else
	mkdir vms || error_exit "创建文件夹vm/vms失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir vm/vms! " >> ${logFile}
fi